
package com.commidea;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;

public class CommideaGatewayClient {

    private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0;

    public CommideaGatewayClient() {
        create0();
        Endpoint CommideaGatewaySoapLocalEndpointEP = service0 .addEndpoint(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoapLocalEndpoint"), new QName("https://www.commidea.webservices.com", "CommideaGatewaySoapLocalBinding"), "xfire.local://CommideaGateway");
        endpoints.put(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoapLocalEndpoint"), CommideaGatewaySoapLocalEndpointEP);
        Endpoint CommideaGatewaySoapEP = service0 .addEndpoint(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoap"), new QName("https://www.commidea.webservices.com", "CommideaGatewaySoap"), "https://txn-test.cxmlpg.com/XML4/commideagateway.asmx");
        endpoints.put(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoap"), CommideaGatewaySoapEP);
    }

    public Object getEndpoint(Endpoint endpoint) {
        try {
            return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
        } catch (MalformedURLException e) {
            throw new XFireRuntimeException("Invalid URL", e);
        }
    }

    public Object getEndpoint(QName name) {
        Endpoint endpoint = ((Endpoint) endpoints.get((name)));
        if ((endpoint) == null) {
            throw new IllegalStateException("No such endpoint!");
        }
        return getEndpoint((endpoint));
    }

    public Collection getEndpoints() {
        return endpoints.values();
    }

    private void create0() {
        TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
        HashMap props = new HashMap();
        props.put("annotations.allow.interface", true);
        AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
        asf.setBindingCreationEnabled(false);
        service0 = asf.create((com.commidea.CommideaGatewaySoap.class), props);
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("https://www.commidea.webservices.com", "CommideaGatewaySoapLocalBinding"), "urn:xfire:transport:local");
        }
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("https://www.commidea.webservices.com", "CommideaGatewaySoap"), "http://schemas.xmlsoap.org/soap/http");
        }
    }

    public CommideaGatewaySoap getCommideaGatewaySoapLocalEndpoint() {
        return ((CommideaGatewaySoap)(this).getEndpoint(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoapLocalEndpoint")));
    }

    public CommideaGatewaySoap getCommideaGatewaySoapLocalEndpoint(String url) {
        CommideaGatewaySoap var = getCommideaGatewaySoapLocalEndpoint();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }

    public CommideaGatewaySoap getCommideaGatewaySoap() {
        return ((CommideaGatewaySoap)(this).getEndpoint(new QName("https://www.commidea.webservices.com", "CommideaGatewaySoap")));
    }

    public CommideaGatewaySoap getCommideaGatewaySoap(String url) {
        CommideaGatewaySoap var = getCommideaGatewaySoap();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }
    
    public static void main(String[] args){
        String sandbox="https://txn-test.cxmlpg.com/XML4/commideagateway.asmx";
        String live="https://txn.cxmlpg.com/XML4/commideagateway.asmx";
        CommideaGatewayClient client=new CommideaGatewayClient();
        CommideaGatewaySoap  soap=client.getCommideaGatewaySoap(sandbox);
        
        Message req=new Message();
        
        ClientHeader header=new ClientHeader();
        header.setSystemID(new BigDecimal("30004220"));
        header.setSystemGUID("E4F4CE97-33D6-4ADD-9E84-D3672DFD662A");
        header.setPasscode("77376534");
        header.setProcessingDB("");
        header.setSendAttempt(0);
        header.setCDATAWrapping(true);
        
        req.setClientHeader(header);
        req.setMsgType("TKI");
        String msgdata="<tokenregistrationrequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"TOKEN\"> ";
        msgdata+="<pan>4111111111111111</pan> ";
        msgdata+="<expirydate>1402</expirydate>";
        msgdata+="<purchase>true</purchase> ";
        msgdata+="<refund>true</refund> ";
        msgdata+="<cashback>true</cashback>";
        msgdata+="<tokenexpirationdate>11122014</tokenexpirationdate>";
        msgdata+="</tokenregistrationrequest>";
        
        req.setMsgData(msgdata);
        
        Message result=soap.processMsg(req);
        String rawResult=result.getMsgData();
        
        Pattern p=Pattern.compile("<CODE>(.*)</CODE>.*<MSGTXT>(.*)</MSGTXT>",Pattern.CASE_INSENSITIVE);
        Matcher m=p.matcher(rawResult);
        if(m.find()){
            //has error
            out(m.group(1)+"="+m.group(2));
        }else{
            //success
            Pattern p2=Pattern.compile("<tokenid>(.*)</tokenid>",Pattern.CASE_INSENSITIVE);
            Matcher m2=p2.matcher(rawResult);
            if(m2.find()){
                out(m2.group(1));
            }else{
                out("!!!!!!!!!!!!!");
            }
        }
        
        //out("!!!-->"+result.getMsgData());
    }
    
    public static void out(Object obj){
        System.out.println(obj);
    }
}
